"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2021/12/16 9:21
@Software: PyCharm
@File    : stdf_variable.py
@Remark  : 
"""
import os
import re
import collections


class ReturnState:
    """:except 异常返回值"""
    PATH_NOT_EXIST = -11
    SUFFIX_NOT_COMPARE = -10
    RUN_SCAN_ERROR = -9

    FTP_SCAN_NO_DOWNLOAD_ALLOW_DOWNLOAD = 11
    FTP_SCAN_ON_DOWNLOAD_NOT_ALLOW_DOWNLOAD = 12
    FTP_SCAN_HAVE_DOWNLOAD_NOT_ALLOW_DOWNLOAD = 13

    FTP_SCAN_NO_UNZIP_ALLOW_UNZIP = 21
    FTP_SCAN_ON_UNZIP_NOT_ALLOW_UNZIP = 22
    FTP_SCAN_HAVE_UNZIP_NOT_ALLOW_UNZIP = 23

    STD_SCAN_NO_ANA_ALLOW_ANA = 31
    STD_SCAN_ON_ANA_NOT_ALLOW_ANA = 32
    STD_SCAN_HAVE_ANA_NOT_ALLOW_ANA = 33

    FILE_SCAN_NO_ANA_ALLOW_ANA = 41
    FILE_SCAN_ON_ANA_NOT_ALLOW_ANA = 42
    FILE_SCAN_HAVE_ANA_NOT_ALLOW_ANA = 43

    UNZIP_7Z_RUN = 80
    UNZIP_ZIP_RUN_SUCCESS = 81
    UNZIP_ZIP_RUN_ERROR = 82

    RUN_SCAN_SUCCESS = 90

    # save_mir_return_mir_id
    FLOW_ID_ANA_FAIL = 101
    MODE_COD_ANA_FAIL = 102
    MIR_ID_GET_SUCCESS = 110
    NO_MIR_ID_FAIL = 111
    MIR_ID_EXISTS = 112
    MIR_ID_NOT_GET = 113
    DEVICE_NOT_GET = 114
    MIR_CANT_COMPARE_SUBCON = 115
    MIR_TO_SQL_ERROR = 116

    # exec_stdf
    EXEC_STDF_SUCCESS = 120
    EXEC_STDF_ERROR = 121
    EXEC_STDF_EMPTY = 122

    HTTP_CONNECT_ERROR = 198
    HTTP_CONNECT_WARNING = 199
    REDIS_NOT_HAVE_CACHE_ZIP = 200
    REDIS_HAVE_CACHE_ZIP = 201
    REDIS_HAVE_CACHE_STD = 202
    REDIS_NOT_HAVE_CACHE_STD = 203
    REDIS_HAVE_CACHE_FILE = 204
    REDIS_NOT_HAVE_CACHE_FILE = 205

    MESSAGE_ERROR = {
        FLOW_ID_ANA_FAIL: "FLOW ID解析失败",
        MODE_COD_ANA_FAIL: "MODE CODE解析失败",
        MIR_ID_GET_SUCCESS: "获取MIR ID成功",
        NO_MIR_ID_FAIL: "MIR ID获取失败",
        MIR_ID_EXISTS: "数据库中存在重复的MIR ID",
        MIR_ID_NOT_GET: "无法从数据库中获取MIR ID",
        DEVICE_NOT_GET: "无法获取到DEVICE名称",
        MIR_CANT_COMPARE_SUBCON: "SUBCON无法和MIR规则匹配",
        MIR_TO_SQL_ERROR: "将MIR ID插入数据库失败",

        # exec_stdf
        EXEC_STDF_SUCCESS: "解析STDF成功",
        EXEC_STDF_ERROR: "解析STDF失败",
        EXEC_STDF_EMPTY: "空的STDF, 无法进行解析",
    }


class GlobalVariable:
    """
    用来放一些全局变量
    TODO: 以大写作为主要的HEAD
    """
    OPEN_SAVE_PKL = False  # 用来将数据保存到二进制数据中用来做APP测试

    STD_SUFFIXES = {".std", ".stdf", ".stdtemp"}

    LOT_TREE_HEAD = [
        "ID", 'R', "LOT_ID", "SB_LOT_ID", "WAFER_ID", "FLOW_ID", "QTY", "PASS", "YIELD", "JOB_NAME", "START_TIME",
        "FINISH_TIME"]
    LOT_TREE_HEAD_LENGTH = len(LOT_TREE_HEAD)

    LimitClass = collections.namedtuple('LimitClass',
                                        ['test_text', 'test_num', 'l_limit', 'h_limit', 'unit', 'test_sort',
                                         'l_limit_type', 'h_limit_type'])  # 新添加两个type
    CACHE_PATH = r"D:\1_STDF\STDF_CACHE"
    JMP_CACHE_PATH = r"D:\1_STDF\JMP_CACHE"
    LIMIT_PATH = r"D:\1_STDF\LIMIT_CACHE"
    DF_COLUMNS = ['ID', 'PART_ID', 'HEAD_NUM', 'SITE_NUM', 'X_COORD', 'Y_COORD', 'NUM_TEST',
                  'HARD_BIN', 'SOFT_BIN', 'PART_FLAG', 'FAIL_FLAG', 'FIRST_FAIL']

    PART_FLAGS = ['ALL', 'FIRST', 'RETEST', 'FINALLY']
    FILE_TABLE_HEAD = ["READ_FAIL", "PART_FLAG", "MESSAGE", "LOT_ID", "SB_LOT_ID", "WAFER_ID", "TEST_CODE", "FLOW_ID",
                       "PART_TYPE", "JOB_NAME", "EQUIPMENT", "SETUP_TIME", "START_TIME", "TEMPERATURE", "FILEPATH"]
    SKIP_FILE_TABLE_DATA_HEAD = {"READ_FAIL", "PART_FLAG"}

    JMP_SCRIPT_HEAD = ["GROUP", "DA_GROUP", "PART_ID", "X_COORD", "Y_COORD", "HARD_BIN", "SOFT_BIN", "FIRST_FAIL"]


class GlobalRePattern:
    """
    用来放一些全局可用的Re Pattern
    """


class RespUrl:
    MAIN_HOST = "http://127.0.0.1:5000"

    """
    传入物料等级
    """
    COMPARE_LOT_SYSTEM_URL = "/home/stdf/compare_system"
    SBL = "/product/sbl"
    LIMIT = '/product/stdf/limit'
    LOT_INFO_URL = "/product/stdf/lot_compare"
    EMAIL_SEND_CONFIG = '/product/email_send'
    COMPARE_MONITOR_URL = "/product/stdf/compare_monitor"
    FIELD_INFO = "/product/stdf/field_info/filed:"
    LOTS_INFO_URL = "/product/stdf/lot_info"
    COMPARE_LOT_LIST_URL = "/product/stdf/compare_lot_list"


def init():
    if not os.path.exists(GlobalVariable.CACHE_PATH):
        os.makedirs(GlobalVariable.CACHE_PATH)
    if not os.path.exists(GlobalVariable.JMP_CACHE_PATH):
        os.makedirs(GlobalVariable.JMP_CACHE_PATH)
    if not os.path.exists(GlobalVariable.LIMIT_PATH):
        os.makedirs(GlobalVariable.LIMIT_PATH)


init()
